package Servlet;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import DAO.DatabaseDAO;
import Flights.Flight;
import Flights.Flights;
import Flights.Passenger;
import Flights.Passengers;

/**
 * Servlet implementation class ReservationServlet
 */
@WebServlet("/ReservationServlet")
public class ReservationServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ReservationServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ServletContext ctx = getServletContext();
		String JMBG = request.getParameter("JMBG");
		String ime = request.getParameter("ime");
		String prezime = request.getParameter("prezime");
		String idLeta = request.getParameter("idLeta");
		String passengerString = "";
		
		PrintWriter out = response.getWriter(); 
		
		String client = request.getParameter("client");
		if(client==null){
			client="web";
		}
		
		HttpSession session = request.getSession();

		Flights flights = new Flights(ctx.getRealPath("") + "/listaLetova/flights.txt");
		Flight flight = flights.getFlights().get(idLeta);
		if(flight==null){
			out.println("Ne postoji let: " + idLeta);
			out.close();
			return;
		}

		Passengers passengers = (Passengers) session.getAttribute("passengers");
		if(passengers==null){
			passengers = new Passengers(ctx.getRealPath("") + "/listaLetova/" + idLeta + ".txt");
		}

		if(flight.getBrojPutnika()-passengers.getPassengers().size() <= 0){
			if(client.equals("CommandPrompt")){
				out.println("Sva mesta na letu: " + idLeta + " su zauzeta");
				out.close();
				return;
			} 
		}
		
		Passenger p = new Passenger(JMBG, ime, prezime);
		
		passengers.setPassenger(p);
		
		//kreiraj string sa podacima o putniku
		passengerString = JMBG + ";" + ime + ";" + prezime;
		
		//sacuvaj podatke o putniku u fajl o letu
		DatabaseDAO dao = DatabaseDAO.getInstance();
		dao.save(passengerString, ctx.getRealPath("")+"/listaLetova/" + idLeta + ".txt");
		
		//ako ne postoji falj o putniku sa njegovim letovima, kreiraj
		File let = new File(ctx.getRealPath("")+"/listaPutnika/" + JMBG + ".txt");
		if(!let.exists()) {
		    let.createNewFile();
		}
		
		//kreiraj string sa podacima o letu
		String flightString = flight.getIdLeta() + ";" + 
				flight.getVremePolaskaString() + ";" + 
				flight.getVremeDolaskaString() + ";" + 
				flight.getPolaziAerodrom() + ";" + 
				flight.getOdrediste() + ";" + 
				flight.getBrojPutnika();
		
		//snimi podatke o letu u fajl o putniku
		dao.save(flightString, ctx.getRealPath("")+"/listaPutnika/" + JMBG + ".txt");
		
		if(client.equals("CommandPrompt")){
			out.println("Uspesno ste rezervisali let: " + idLeta + " za putnika " + ime + " " + prezime);
			out.close();
		} else {
			RequestDispatcher disp = getServletContext().getRequestDispatcher("/sediste_rezervisano.faces");
			disp.forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
